﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml.Serialization;
using System.IO;

namespace Com.Becom.CommonLibrary.Common
{

    /// <summary>
    /// 存放系统公共方法
    /// </summary>
    public class ToolClass
    {

        /// <summary>
        /// 判断是否为数字
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool IsNum(object obj)
        {
            bool result = true;
            string str = obj as string;
            if (str == null)
            {
                result = false;
            }
            else
            {
                foreach (char c in str)
                {
                    if (c < '0' || c > '9')
                    {
                        result = false;
                    }
                }
                if (string.IsNullOrEmpty(str))
                {
                    result = false;
                }
            }
            return result;
        }

        /// <summary>
        /// 判断数据集是否为空。
        /// </summary>
        /// <param name="dataSet"></param>
        /// <returns></returns>
        public static bool IsDataSetEmpty(DataSet dataSet)
        {
            if (dataSet == null || dataSet.Tables.Count == 0 || dataSet.Tables[0].Rows.Count == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 以某一个字段的某个值为标准，筛选出等于此值的所有行记录
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="str_ColumnName">字段名</param>
        /// <param name="str_ColumnValue">值</param>
        /// <returns></returns>
        public static DataSet GetSelect(DataSet ds, string str_ColumnName, string str_ColumnValue)
        {
            DataSet ds_Return = new DataSet();
            ds_Return.EnforceConstraints = false;
            DataTable dt_Return = new DataTable();
            try
            {
                dt_Return = ds.Tables[0].Clone();
                ds_Return.Tables.Add(dt_Return);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (ds.Tables[0].Rows[i][str_ColumnName].ToString() ==
                        str_ColumnValue)
                    {

                        ds_Return.Tables[0].ImportRow(ds.Tables[0].Rows[i]);

                    }
                }
            }
            catch (Exception exp)
            {

            }
            return ds_Return;

        }

        /// <summary>
        /// 返回与传入的两个字段名对应的值相等的所有记录
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="str_ColumnName"></param>
        /// <param name="str_ColumnValue"></param>
        /// <param name="str_ColumnName1"></param>
        /// <param name="str_ColumnValue1"></param>
        /// <returns></returns>
        public static DataSet GetSelect(DataSet ds, string str_ColumnName, string str_ColumnValue, string str_ColumnName1, string str_ColumnValue1)
        {
            DataSet ds_Return = new DataSet();
            ds_Return = ds.Clone();
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr[str_ColumnName].ToString() == str_ColumnValue &&
                    dr[str_ColumnName1].ToString() == str_ColumnValue1)
                {
                    ds_Return.Tables[0].ImportRow(dr);
                }
            }
            return ds_Return;
        }

        /// <summary>
        /// 返回集合中指定列等于指定值的其它列的值
        /// </summary>
        /// <param name="ds"></param>
        /// <param name="ColumnName"></param>
        /// <param name="ColumnReturnName"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetColumnValue(DataSet ds, string ColumnName, string ColumnReturnName, string value)
        {
            string returnValue = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr[ColumnName].ToString() == value)
                {
                    returnValue = dr[ColumnReturnName].ToString();
                    break;
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 计算当前和start相差的时间
        /// </summary>
        /// <param name="start"></param>
        /// <returns></returns>
        public static string CalcExecTime(ref DateTime start)
        {
            DateTime cur = DateTime.Now;
            TimeSpan ts = cur.Subtract(start);
            start = cur;
            return Convert.ToString(ts.TotalMilliseconds);
        }

        public static string obj2xml(object obj)
        {
            XmlSerializer xs = new XmlSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            xs.Serialize(ms, obj);
            byte[] tBytes = ms.ToArray();
            string a = System.Text.Encoding.UTF8.GetString(tBytes);
            ms.Close();
            return a;
        }

        public static object xml2obj(string a, Type type)
        {
            byte[] tBytes = System.Text.Encoding.UTF8.GetBytes(a);
            XmlSerializer xs = new XmlSerializer(type);
            MemoryStream ms = new MemoryStream();
            ms.Write(tBytes, 0, tBytes.Length);
            ms.Position = 0;
            object obj = xs.Deserialize(ms);
            ms.Close();
            return obj;
        }
    }
}
